package cn.gov.pbc.dream.pojo.bo;

import cn.gov.pbc.dream.util.DateTimeUtil;
import lombok.Data;
import lombok.ToString;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.Buffer;
import java.util.Date;

@Data
@ToString
public class PPIMessageBo {

    private String orgCode;

    private Date reportDate;

    private PPB1Bo ppb1Bo;

    private PIB1Bo pib1Bo;

    private PIB2Bo pib2Bo;

    private PIE1Bo pie1Bo;

    private PPE1Bo ppe1Bo;

    public PPIMessageBo() {
        this.ppb1Bo = new PPB1Bo();
        this.pib1Bo = new PIB1Bo();
        this.pib2Bo = new PIB2Bo();
        this.pie1Bo = new PIE1Bo();
        this.ppe1Bo = new PPE1Bo();
    }

    public void addPPB1InputStream(InputStream is) throws UnsupportedEncodingException {
        this.ppb1Bo.getBufferedReaderList().add(
                new BufferedReader(new InputStreamReader(is, "gb2312")));
    }

    public void addPIB1InputStream(InputStream is) throws UnsupportedEncodingException {
        this.pib1Bo.getBufferedReaderList().add(
                new BufferedReader(new InputStreamReader(is, "gb2312"))
        );
    }

    public void addPIB2InputStream(InputStream is) throws UnsupportedEncodingException {
        this.pib2Bo.getBufferedReaderList().add(
                new BufferedReader(new InputStreamReader(is, "gb2312"))
        );
    }

    public void addPIE1InputStream(InputStream is) throws UnsupportedEncodingException {
        this.pie1Bo.getBufferedReaderList().add(
                new BufferedReader(new InputStreamReader(is, "gb2312"))
        );
    }

    public void addPPE1InputStream(InputStream is) throws UnsupportedEncodingException {
        this.ppe1Bo.getBufferedReaderList().add(
                new BufferedReader(new InputStreamReader(is, "gb2312"))
        );
    }

    public void assemblePPB1Bo(String data) {
        String[] values = data.split("\\|", 4);
        PPB1Bo.PPBItem item = ppb1Bo.new PPBItem();
        item.setCode(values[0]);
        item.setOrgCode(values[1]);
        item.setName(values[2]);
        item.setStartDate(DateTimeUtil.strToDate(values[3], DateTimeUtil.DASH_DATE_FORMAT));
        ppb1Bo.getItemList().add(item);
    }

    public void assemblePIB1Bo(String data) {
        this.pib1Bo.assembleData(data);
    }

    public void assemblePIB2Bo(String data) {
        this.pib2Bo.assembleProductRaiseInfoList(data);
    }

    public void assemblePIE1Bo(String data) {
        this.pie1Bo.assembleTerminationInfo(data);
    }

    public void assemblePPE1Bo(String data){
        this.ppe1Bo.assembleAssetPoolTerminationInfo(data);
    }
}
